Framework "4.0"

properties {
    $BaseDir = Resolve-Path "."
    $SolutionFile = "$BaseDir\LibraryGuard.sln"
    $DeployDir = "$BaseDir\Deploy\"
    $Tools = "$BaseDir\Tools\"
    $Configuration = "Release"
    $Platform = "Any CPU"
    $BinDir = "$BaseDir\Bin\$Platform\$Configuration"
	
	$7z = "$Tools\7za.exe"
	$md2html = "$Tools\md2html.ps1"
    
    $DeployPrefix = "LibraryGuard."
    
    $BinFiles = @("LibraryGuard.exe", "FLib.dll", "Irony.dll", "NLog.dll", "CmdLine.dll", "taglib-sharp.dll")
    $MiscFiles = @("$BaseDir\Readme.md", "$BaseDir\License.txt", "$BaseDir\Src\LibraryGuard\SampleConfig.xml")
    $NLogConfigFileDest = "NLog.config"
	
	$MDHeader = "$BaseDir\Src\ReadmeHeader.html"
	$MDFooter = "$BaseDir\Src\ReadmeFooter.html"
	
    $Version = ""
    $NLogConfig = ""
    $OutputDir = ""
}

Task Default -Depends Deploy

Task Deploy -Depends Build, Pack {
}

Task Build -Depends Clean, Compile, GetVersion {
}

Task Compile {
    msbuild /p:"Configuration=$Configuration" /p:"Platfrom=$Platform" $SolutionFile
}

Task Clean {
    msbuild /p:"Configuration=$Configuration" /p:"Platfrom=$Platform" $SolutionFile /target:Clean
}

Task GetVersion -Depends Compile {
    $script:Version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$BinDir\LibraryGuard.exe").ProductVersion.ToString().TrimEnd('0', '.')
}

Task PrepareDeploy {
    # NLog.config for Debug or NLog.$Configuration.config
    if($Configuration -eq "Debug")
    {
        $script:NLogConfig = "NLog.config"
    }
    else
    {
        $script:NLogConfig = "NLog.$Configuration.config"
    }
    
    if(-not(Test-Path $DeployDir))
    {
        New-Item $DeployDir -Type Directory | Out-Null
    }

    $script:OutputDir = $DeployDir + $DeployPrefix + $script:Version + "\"
    if(Test-Path $script:OutputDir)
    {
        Remove-Item -Recurse -Force $script:OutputDir | Out-Null
    }
    New-Item $script:OutputDir -Type Directory | Out-Null
}

Task DeployFiles -Depends PrepareDeploy {
    foreach($file in $BinFiles)
    {
        Copy-Item "$BinDir\$file" "$script:OutputDir\$file"
    }
    foreach($file in $MiscFiles)
    {
        Copy-Item "$file" ("$script:OutputDir\" + [System.IO.Path]::GetFileName($file))
    }
    Copy-Item "$BinDir\$script:NLogConfig" "$script:OutputDir\$NLogConfigFileDest"
	
	$input = "$script:OutputDir\Readme.md"
	$output = "$script:OutputDir\Readme.html"
	&$md2html -InputFile $input -OutputFile $output -HeaderFile $MDHeader -FooterFile $MDFooter
}

Task Pack -Depends DeployFiles {
    $archive = $DeployDir + $DeployPrefix + $script:Version + ".zip"
    if(Test-Path $archive)
    {
        Remove-Item -Force $archive | Out-Null    
    }
    Exec { &$7z a $archive $script:OutputDir }
}